const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    quantity: 1,
    id: '',
    skuCode: '',
    skuName: '',
    model: '',
    attr1:'',
    taxRate: 1,
    factoryPrice: 0,
    saleFee: 0,
    costFee: parseFloat(0).toFixed(2),
    excludingTaxCosts: 0,
    excludingTaxCostsFee: 0,
    jingshouru: 0,
    yunfeil: parseFloat(5).toFixed(2),
    yunfeie: 0,
    maolie: 0,
    maolil: 0,
    cuxiaofeiyonge: 0,
    cuxiaofeiyongl: parseFloat(10).toFixed(2),
    pingtaifeiyonge: 0,
    pingtaifeiyongl: parseFloat(6).toFixed(2),
    shuijinfujiae: 0,
    shuijinfujial: parseFloat(0).toFixed(2),
    zijinlixie: parseFloat(0).toFixed(2),
    zijinlixil: 0,
    qitafeiyonge: 0,
    qitafeiyongl: parseFloat(0).toFixed(2),
    xingzhengfeiyonge: parseFloat(0).toFixed(2),
    xingzhengfeiyongl: 0,
    yongjinfeiyonge: 0,
    yongjinfeiyongl: parseFloat(2).toFixed(2),
    kengweifeiyonge: parseFloat(0).toFixed(2),
    kengweifeiyongl: 0,
    feiyongxiaojie:0,
    feiyongxiaojil:0,
    feiyonghejie:0,
    feiyonghejil:0,
    lirune: 0,
    lirunl: 0
  },
  onInputChange(e) {
    const costFee = this.data.costFee
    const cuxiaofeiyongl = this.data.cuxiaofeiyongl
    const pingtaifeiyongl = this.data.pingtaifeiyongl
    const shuijinfujial = this.data.shuijinfujial
    const zijinlixie = this.data.zijinlixie
    const qitafeiyongl = this.data.qitafeiyongl
    const yongjinfeiyongl = this.data.yongjinfeiyongl
    const kengweifeiyonge = this.data.kengweifeiyonge
    const xingzhengfeiyonge = this.data.xingzhengfeiyonge
    const factoryFee = this.data.factoryPrice * this.data.quantity
    const saleFee = factoryFee - this.data.costFee
    const excludingTaxCostsFee = this.data.excludingTaxCosts * this.data.quantity
    const jingshouru = saleFee / this.data.taxRate
    const yunfeie = factoryFee * this.data.yunfeil * 0.01
    const maolie = jingshouru - excludingTaxCostsFee - yunfeie
    const maolil = saleFee === 0 ? 0 : maolie / saleFee / 0.01
    const cuxiaofeiyonge = factoryFee * cuxiaofeiyongl * 0.01
    const pingtaifeiyonge = factoryFee * pingtaifeiyongl * 0.01
    const shuijinfujiae = 0 - factoryFee * shuijinfujial * 0.01
    const zijinlixil = zijinlixie / factoryFee * 100
    const qitafeiyonge = qitafeiyongl * factoryFee * 0.01
    const xingzhengfeiyongl = xingzhengfeiyonge / factoryFee * 100
    const yongjinfeiyonge = yongjinfeiyongl * factoryFee * 0.01
    const kengweifeiyongl = kengweifeiyonge / factoryFee * 100
    const feiyongxiaojie  = Number(cuxiaofeiyonge) + Number(pingtaifeiyonge) + Number(shuijinfujiae) + Number(zijinlixie) + Number(qitafeiyonge) + Number(yongjinfeiyonge) + Number(kengweifeiyonge)
    const feiyongxiaojil = feiyongxiaojie / factoryFee / 0.01
    const feiyonghejie = Number(feiyongxiaojie)+Number(xingzhengfeiyonge)
    const feiyonghejil = feiyonghejie/factoryFee/0.01
    const lirune = maolie - cuxiaofeiyonge - pingtaifeiyonge - shuijinfujiae - zijinlixie - qitafeiyonge - xingzhengfeiyonge - yongjinfeiyonge - kengweifeiyonge
    const lirunl = lirune / factoryFee / 0.01
    this.setData({
      factoryFee: factoryFee,
      costFee: costFee,
      saleFee: saleFee,
      excludingTaxCostsFee: excludingTaxCostsFee,
      jingshouru: jingshouru,
      yunfeie: yunfeie,
      maolie: maolie,
      maolil: maolil,
      cuxiaofeiyonge: cuxiaofeiyonge,
      pingtaifeiyonge: pingtaifeiyonge,
      shuijinfujiae: shuijinfujiae,
      zijinlixil: zijinlixil,
      qitafeiyonge: qitafeiyonge,
      xingzhengfeiyongl: xingzhengfeiyongl,
      yongjinfeiyonge: yongjinfeiyonge,
      kengweifeiyongl: kengweifeiyongl,
      feiyongxiaojie: feiyongxiaojie,
      feiyongxiaojil: feiyongxiaojil,
      feiyonghejie: feiyonghejie,
      feiyonghejil: feiyonghejil,
      lirune: lirune,
      lirunl: lirunl
    })
  },

  onInputBlur(e) {
    const fieldName = e.currentTarget.dataset.id
    let value = e.detail.value
    if (!value || isNaN(value)) {
      value = 0
    }
    value = parseFloat(value).toFixed(2)
    if (fieldName) {
      const changeData = {}
      changeData[fieldName] = value
      this.setData(changeData)
    }
  },
  getData(id) {
    app.func.request({
      url: '/finance/budget/page',
      data: {
        baseParams: {
          id: id
        }
      },
      success: ({
        data
      }) => {
        const row = data[0] || {}
        this.setData({
          id: row.id,
          skuCode: row.skuCode,
          skuName: row.skuName,
          skuTitle: row.skuTitle,
          model: row.model,
          attr1: row.attr1,
          taxRate: row.taxRate,
          factoryPrice: row.factoryPrice,
          saleFee: row.factoryPrice,
          excludingTaxCosts: row.excludingTaxCosts,
          quantity: 1,
        })
        this.onInputChange()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.id) {
      const id = options.id
      this.getData(id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})